public class Quick {
    public void sort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return;
        }
        quickSort(arr, 0, arr.length - 1);
    }

    private void quickSort(int[] arr, int left, int right) {
        if (left < right) {
            int partitionIndex = partition(arr, left, right);
            quickSort(arr, left, partitionIndex - 1);
            quickSort(arr, partitionIndex + 1, right);
        }
    }

    private int partition(int[] arr, int left, int right) {
        int pivot = arr[right]; // 选择最右边的元素作为基准值
        int i = left - 1; // 定义分区点，并初始化为左边界-1
        for (int j = left; j < right; j++) {
            if (arr[j] < pivot) {
                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, right); // 将基准值放到合适的位置
        return i + 1; // 返回基准值的最终位置
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args){
        int[] arr={2,14,2,3,5,8,5,9,6,4,3};
        Quick q=new Quick();
        q.sort(arr);
        for (int j : arr) {
            System.out.print(j + " ");
        }
    }
}
